package com.isscollage.service.impl;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.isscollage.dao.OrderDao;
import com.isscollage.dao.TicketDao;
import com.isscollage.dao.UserDao;
import com.isscollage.entity.OrderDetail;
import com.isscollage.entity.OrderHeader;
import com.isscollage.entity.Passenger;
import com.isscollage.entity.Ticket;
import com.isscollage.entity.TicketOrder;
import com.isscollage.entity.Users;
import com.isscollage.search.OrderSearch;
import com.isscollage.service.OrderService;
import com.isscollage.service.UserService;
import com.isscollage.utils.UUIDUtils;

@Service
public class OrderServiceImpl implements OrderService{
	
	@Autowired
	private OrderDao orderDao;
	@Autowired
	private HttpSession session;
	@Autowired
	private TicketDao ticketDao;
	@Autowired
	private UserDao userDao;
	@Override
	public List<TicketOrder> searchTicketOrderByCon(OrderSearch orderSearch) {
		// TODO Auto-generated method stub
		return orderDao.searchTicketOrderByCon(orderSearch);
	}
	@Override
	public TicketOrder getTicketOrderById(String ticketOrderId) {
		// TODO Auto-generated method stub
		return orderDao.getTicketOrderById(ticketOrderId);
	}
	@Override
	@Transactional
	public OrderHeader saveOrder(OrderHeader orderHeader) {
		// TODO Auto-generated method stub
		orderHeader.setOrderId(UUIDUtils.getUUID());
		orderHeader.setCreateTime(new Date());
		orderHeader.setOrderDate(new Date());
		orderHeader.setOrderFlag(0);
		Users user=(Users) session.getAttribute("user");
		orderHeader.setCreateId(user.getUserId());
		int orderNumber=0;
		TicketOrder ticketOrder=orderHeader.getTicketOrder();
		if(ticketOrder!=null && orderHeader.getTicketOrder().getId()!=null){
			ticketOrder=orderDao.getTicketOrderById(orderHeader.getTicketOrder().getId());
			orderHeader.setStartTime(ticketOrder.getStartTime());
		}	
		if(orderHeader.getTicket()!=null && orderHeader.getTicket().getTicketId()!=null){
			Ticket ticket=ticketDao.getTicketById(orderHeader.getTicket().getTicketId());
			orderHeader.setTicket(ticket);
			if(orderHeader.getOrderDetail()!=null){
				orderNumber=orderHeader.getOrderDetail().size();//有几条信息就有几个乘车人，就有几张票
				orderHeader.setOrderNumber(orderHeader.getOrderDetail().size());
				orderHeader.setOrderPrice(ticket.getTicketPrice()*orderNumber);
				orderDao.saveOrder(orderHeader);
				for (OrderDetail orderDetail : orderHeader.getOrderDetail()) {
					if(orderDetail.getPassenger()!=null && orderDetail.getPassenger().getPassengerId()!=null){
						orderDetail.setDetailId(UUIDUtils.getUUID());
						orderDetail.setOrderHeader(orderHeader);
						Passenger passenger=userDao.getPassengerById(orderDetail.getPassenger().getPassengerId());
						orderDetail.setPassenger(passenger);
						orderDetail.setOrderPrice(ticketOrder.getTicketPrice());
						orderDetail.setDetailFlag(0);												
						orderDao.saveOrderDetail(orderDetail);						
					}
				}
			}
			ticketOrder.setTicketNumber((ticketOrder.getTicketNumber()-orderNumber));//修改车票数量
			orderDao.updateTicketOrder(ticketOrder);
		}
		return orderHeader;
	}
	@Override
	@Transactional
	public boolean payOrder(String orderId) {
		// TODO Auto-generated method stub
		OrderHeader orderHeader=orderDao.getOrderByOrderId(orderId);
		Users user=(Users) session.getAttribute("user");
		if(orderHeader!=null){
			double account=user.getAccountNo()-orderHeader.getOrderPrice();
			user.setAccountNo(account);
			userDao.updateUser(user);
			//1代表已付款未出行
			orderDao.updateOrderFlagByOrderId(1,orderId);
			return true;
		}
		return false;
	}
	@Override
	//唯一性
	@Transactional
	public boolean cancelOrder(String orderId) {
		// TODO Auto-generated method stub
		OrderHeader orderHeader = orderDao.getOrderByOrderId(orderId);
		if(orderHeader!=null){
			if(orderHeader.getOrderDetail()!=null){
				//查询出记录数
				int count=orderHeader.getOrderDetail().size();
				//删除两张表数据
				orderDao.deleteOrderHeader(orderId);
				orderDao.deleteOrderDetail(orderId);
				//获取车票订单信息
				List<TicketOrder> list = orderDao.getTicketOrderByCon(orderHeader.getTicket().getTicketId(),orderHeader.getStartTime());
				if(list!=null && list.size()>0){
					//获取取出的第一个元素					
					TicketOrder ticketOrder=list.get(0);
					ticketOrder.setTicketNumber(ticketOrder.getTicketNumber()+count);
					orderDao.updateTicketOrder(ticketOrder);
					return true;
				}
			}
		}
		return false;
	}
	@Override
	public boolean checkOrder() {
		// TODO Auto-generated method stub
		Users user = (Users) session.getAttribute("user");
		if(user!=null){
			List<OrderHeader> list=orderDao.getOrderByUserId(user.getUserId());
			if(list!=null && list.size()>0){
				return true;
			}
		}
		return false;
	}
	@Override
	public List<OrderHeader> searchOrderByCon(OrderSearch search) {
		// TODO Auto-generated method stub
		Users user=(Users)session.getAttribute("user");
		if(user!=null){
			search.setUserId(user.getUserId());
			List<OrderHeader> list=orderDao.searchOrderByCon(search);
			return list;
		}
		return null;
	}
	@Override
	@Transactional
	public boolean returnTicket(String detailId) {
		// TODO Auto-generated method stub
		try {
			OrderDetail detail=orderDao.getOrderDetailByDetailId(detailId);
			//将detailFlag变为3 已退款
			boolean flag=orderDao.updateOrderByDetailId(detailId);
			if(flag){
				OrderHeader header=orderDao.getOrderByOrderId(detail.getOrderHeader().getOrderId());
				orderDao.updateHeaderFlagByOrderId(header.getOrderId());
				header.setOrderPrice(header.getOrderPrice()-detail.getOrderPrice());
				header.setOrderNumber(header.getOrderNumber()-1);
				orderDao.updateOrderHeader(header);
				List<TicketOrder> list=orderDao.getTicketOrderByCon(header.getTicket().getTicketId(),header.getStartTime());
				if(list!=null && list.size()>0){
					TicketOrder ticketOrder =list.get(0);
					ticketOrder.setTicketNumber(ticketOrder.getTicketNumber()+1);
					orderDao.updateTicketOrder(ticketOrder);
					Users user=(Users) session.getAttribute("user");
					user.setAccountNo(user.getAccountNo()+detail.getOrderPrice());
					userDao.updateUser(user);
					return true;
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	@Override
	public boolean gotoTicket(String detailId) {
		// TODO Auto-generated method stub
		OrderDetail detail=orderDao.getOrderDetailByDetailId(detailId);
		//将detailFlag改为2
		boolean flag=orderDao.updateDetailByDetailId(detailId);
		if(flag){
			OrderHeader header=orderDao.getOrderByOrderId(detail.getOrderHeader().getOrderId());
			if(header!=null){
				orderDao.updateHeaderFlagByOrderId(header.getOrderId());
				return true;
			}
		}
		return false;
	}
	@Override
	public boolean deleteTicket(String detailId) {
		// TODO Auto-generated method stub
		OrderDetail detail=orderDao.getOrderDetailByDetailId(detailId);
		boolean flag=orderDao.deleteOrderDetailByDetailId(detailId);
		if(flag){
			OrderHeader header=orderDao.getOrderByOrderId(detail.getOrderHeader().getOrderId());
			if(header!=null){
				orderDao.deleteOrderHeader(header.getOrderId());
				return true;
			}
		}
		return false;
	}
	

}
